package com.gupaoedu.concurrent.thread.example;

import com.gupaoedu.concurrent.thread.example.processor.FlightProcessor;
import com.gupaoedu.concurrent.thread.example.processor.HotelProcessor;
import com.gupaoedu.concurrent.thread.example.processor.Processor;
import com.gupaoedu.concurrent.thread.example.processor.TrainProcessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/5/12
 * created by chenpp
 * 对于行程的数据  机票/火车/酒店 通过不同的Processor在多线程下同时进行处理,相比较
 * 顺序处理数据可以提高效率
 */
public class ItineraryHandlersTest {
    private static Map<Processor, List<Itinerary>> map = new HashMap<Processor,List<Itinerary>>();
    public static void main(String[] args) {
        //创建Processor和行程集合对应的Map
        addHandlerAndList(new TrainProcessor(),"train");
        addHandlerAndList(new FlightProcessor(),"flight");
        addHandlerAndList(new HotelProcessor(),"hotel");
        System.out.println("处理前数据:");
        for(Processor processor:map.keySet()){
            List<Itinerary> list = map.get(processor);
            list.stream().forEach(itinerary->System.out.println(itinerary));
        }
        for(Processor processor:map.keySet()){
            new Thread(()->processor.handler(map.get(processor))).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(Processor processor:map.keySet()){
            List<Itinerary> list = map.get(processor);
            list.stream().forEach(itinerary->System.out.println(itinerary));
        }

    }
    private static void addHandlerAndList(Processor processor,String scheduleType){
        List<Itinerary> list = new ArrayList<Itinerary>();
        list.add(new Itinerary(scheduleType,"123"));
        list.add(new Itinerary(scheduleType,"124"));
        map.put(processor,list);
    }
}
